/**
 * 
 */
package com.dumai.babymusic.db;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * @author LiLiu
 * 
 */
public class DBAdapter extends SQLiteOpenHelper {

	public static final String TB_MUSIC = "tb_music";
	public static final String TB_FAVOR = "tb_favorite";

	public static final String FIELD_ID = "ID";
	public static final String FIELD_YUNZHOU = "YunZhou";
	public static final String FIELD_TITLE = "Title";
	public static final String FIELD_URL = "Url";
	public static final String FIELD_DESC = "Desc";


	private static final String TAG = "DBAdapter";
	private static final String DATABASE_NAME = "babymusicdb.db";
	private static final int DATABASE_VERSION = 1;

	// private static final String DATABASE_CREATE =
	// "create table tb_music (_id integer primary key autoincrement, "
	// + "isbn text not null, title text not null, "
	// + "publisher text not null);";

	private SQLiteDatabase db;

	public DBAdapter(Context ctx) {
		super(ctx, DATABASE_NAME, null, DATABASE_VERSION);

	}

	// ---打开数据库---
	public DBAdapter open() throws SQLException {

		db = this.getWritableDatabase();
		return this;
	}

	public void close() {
		db.close();
	}

	// ---向数据库中插入一个---
	public long insertMusic(MusicEntity eMusic) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(FIELD_ID, eMusic.getId());
		initialValues.put(FIELD_YUNZHOU, eMusic.getYunzhou());
		initialValues.put(FIELD_TITLE, eMusic.getTitle());
		initialValues.put(FIELD_URL, eMusic.getUrl());
		initialValues.put(FIELD_DESC, eMusic.getDesc());
		return db.insert(TB_MUSIC, null, initialValues);
	}

	//
	public long insertFavorMusic(String id) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(FIELD_ID, id);
		return db.insert(TB_FAVOR, null, initialValues);
	}

	// ---删除一个指定---
	public boolean deleteMusic(String id) {
		boolean result = db.delete(TB_MUSIC, FIELD_ID + "='" + id + "'", null) > 0;

		// result = db.delete(TB_FAVOR, FIELD_ID + "='" + id + "'", null) > 0;

		return result;
	}

	//
	public ArrayList<MusicEntity> getFavorMusic() {
		ArrayList<MusicEntity> list = new ArrayList<MusicEntity>();

		Cursor c = db
				.rawQuery(
						"select a.id, a.yunzhou, a.title, a.url, a.desc from tb_music a, tb_favorite b where b.id=a.id order by b.id",
						null);

		if (c != null && c.moveToFirst()) {
			do {
				MusicEntity me = new MusicEntity();

				me.setId(c.getString(0));
				me.setYunzhou(c.getInt(1));
				me.setTitle(c.getString(2));
				me.setUrl(c.getString(3));
				me.setDesc(c.getString(4));

				list.add(me);

			} while (c.moveToNext());
		}

		return list;
	}

	// ---检索所有---
	public ArrayList<MusicEntity> getAllMusic() {
		ArrayList<MusicEntity> list = new ArrayList<MusicEntity>();

		Cursor c = db.query(TB_MUSIC, new String[] { FIELD_ID, FIELD_YUNZHOU,
				FIELD_TITLE, FIELD_URL, FIELD_DESC }, null, null, null, null,
				null);

		if (c != null && c.moveToFirst()) {
			do {
				MusicEntity me = new MusicEntity();

				me.setId(c.getString(0));
				me.setYunzhou(c.getInt(1));
				me.setTitle(c.getString(2));
				me.setUrl(c.getString(3));
				me.setDesc(c.getString(4));

				list.add(me);

			} while (c.moveToNext());
		}

		return list;
	}

	// ---更新一个标题---
	public boolean updateMusic(MusicEntity me) {
		ContentValues args = new ContentValues();
		args.put(FIELD_YUNZHOU, me.getYunzhou());
		args.put(FIELD_TITLE, me.getTitle());
		args.put(FIELD_URL, me.getUrl());
		args.put(FIELD_DESC, me.getDesc());
		return db.update(TB_MUSIC, args, FIELD_ID + "='" + me.getId() + "'",
				null) > 0;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// // db.execSQL(DATABASE_CREATE);

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
				+ newVersion + ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS tb_music");
		onCreate(db);

	}

}
